package com.yunkeji.api.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunkeji.api.common.service.SysParamService;
import com.yunkeji.common.constant.CacheNames;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.sys.SysConfig;
import com.yunkeji.dao.mapper.sys.SysConfigMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class SysParamServiceImpl implements SysParamService<T> {

    @Autowired SysConfigMapper sysConfigMapper;

    @Autowired @Lazy SysParamService<T> sysParamService;
    /**
     * 根据键名查询参数配置信息
     *
     * @param configKey 参数key
     * @return 参数键值
     */
    @Cacheable(cacheNames = CacheNames.SYS_CONFIG, key = "#configKey")
    @Override
    public String selectConfigByKey(String configKey) {
        SysConfig retConfig =
                sysConfigMapper.selectOne(
                        new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getConfigKey, configKey));
        if (ObjectUtil.isNotNull(retConfig)) {
            return retConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }

    @Override
    public T selectConfigByKey(String configKey, T defaultV) {
        String byKey = sysParamService.selectConfigByKey(configKey);
        return StrUtil.isEmpty(byKey) ? defaultV : BeanUtil.toBean(byKey, defaultV.getClass());
    }
}
